package com.exam.system.controller;

import com.exam.system.entity.*;
import com.exam.system.service.ExamInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api("考试信息管理")
@RestController
@CrossOrigin
@RequestMapping("/exam/")
public class ExamInfoController {
    @Autowired
    ExamInfoService examInfoService;

    @ApiImplicitParam(name = "pageSize", value = "页尺寸")
    @ApiOperation(value = "考试信息列表")
    @RequestMapping("examList")
    public ResponseEntity<PageInfo> findAll(Integer pageNum, Integer pageSize, Integer examTypeId) {
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<ExamInfo> info = null;
        if (examTypeId != null) {
            List<ExamInfo> list = examInfoService.selectByTypeId(examTypeId);
            info = new PageInfo<>(list);
        } else {
            List<ExamInfo> list = examInfoService.findAll();
            info = new PageInfo<>(list);
        }
        return new ResponseEntity<>(200, "", info);
    }



    @ApiImplicitParams({@ApiImplicitParam(value = "根据类别查询参数", name = "type"),
            @ApiImplicitParam(value = "根据开考时间查询参数", name = "beginTime"),
            @ApiImplicitParam(value = "根据内容查询参数", name = "searchText"),
            @ApiImplicitParam(value = "中间参数选择根据什么查询", name = "selectItem")})
    @ApiOperation(value = "查询后的考试信息列表")
    @RequestMapping("examlist2")
    public ResponseEntity<PageInfo<ExamInfo>> list2(int pageNum, int pageSize, String selectItem, String searchText, String beginTime, Integer type) {
        ExamInfo examInfo = new ExamInfo();
        PageHelper.startPage(pageNum, pageSize);
        //确定了
        if (selectItem.equals("1")) {
            examInfo.setExamsName(searchText);
            PageHelper.startPage(pageNum, pageSize);
            List<ExamInfo> checklist = examInfoService.selectByExamName(examInfo);
            PageInfo<ExamInfo> checkPageInfo = new PageInfo<>(checklist);
            return new ResponseEntity(200, "", checkPageInfo);
        } else if (selectItem.equals("2")) {
            List<ExamInfo> list = examInfoService.selectByBeginTime(beginTime);
            PageInfo<ExamInfo> checkPageInfo = new PageInfo<>(list);
            return new ResponseEntity(200, "", checkPageInfo);
        } else if (selectItem.equals("3")) {
            PageHelper.startPage(pageNum, pageSize);
            List<ExamInfo> checklist = examInfoService.selectByTypeId(type);
            PageInfo<ExamInfo> checkPageInfo = new PageInfo<>(checklist);
            return new ResponseEntity(200, "", checkPageInfo);
        } else {
            PageHelper.startPage(pageNum, pageSize);
            List<ExamInfo> checklist = examInfoService.findAll();
            PageInfo<ExamInfo> checkPageInfo = new PageInfo<>(checklist);
            return new ResponseEntity(200, "", checkPageInfo);
        }
    }


    @ApiImplicitParam(name = "id", value = "考试信息id")
    @ApiOperation(value = "查看某条考试信息")
    @RequestMapping("/examview")
    public ResponseEntity<ExamInfo> examView(Integer id) {
        try {
            examInfoService.selectByPrimaryKey(id);
            return new ResponseEntity<>(200, "查看成功", null);
        } catch (Exception e) {
            return new ResponseEntity<>(400, "查看失败", null);
        }
    }

    //删除一条工单信息
    @ApiImplicitParam(name = "id", value = "考试信息id")
    @ApiOperation(value = "删除一条考试信息")
    @RequestMapping("/examdelete")
    public ResponseEntity<ExamInfo> examInfoDelete(Integer id) {
        examInfoService.deleteById(id);
        return new ResponseEntity<>(200, "删除成功", null);

    }

    @ApiImplicitParam(name = "id", value = "考试信息id")
    @ApiOperation(value = "修改一条考试信息")
    @RequestMapping("/examupdate")
    public ResponseEntity<ExamInfo> examInfoUpdate(@RequestBody ExamInfo examInfo) {
        examInfoService.updateById(examInfo);
        return new ResponseEntity<>(200, "修改成功", null);

    }

    //添加
    @ApiImplicitParam(name = "examInfo", value = "考试信息实体类")
    @ApiOperation(value = "添加考试方法")
    @RequestMapping("/add")
    public ResponseEntity<ExamInfo> examInfoAdd(@RequestBody ExamInfo examInfo) {
        examInfoService.insert(examInfo);
        return new ResponseEntity<>(200, "添加成功", null);
    }

    @ApiOperation(value = "查询所有的学员")
    @RequestMapping("/findalluser")
    public ResponseEntity<List<ExamUser>> findAllUser() {

        return new ResponseEntity<>(200, "", examInfoService.findAllUser());
    }

    //打开添加页面
    @ApiOperation(value = "打开添加考试页面")
    @RequestMapping("/findpapername")
    public ResponseEntity< Map<String,List>> examInfoAddPaper() {
        Map<String,List> map=new HashMap<>();
        map.put("paper",examInfoService.findPaper());
        map.put("type",examInfoService.findExamType());
        return new ResponseEntity<>(200, "",map);
    }

    @ApiOperation(value = "查询所有的考试类型")
    @RequestMapping("/findalltype")
    public ResponseEntity<List<ExamType>> findAllType() {
        return new ResponseEntity<>(200, "", examInfoService.findExamType());
    }
}